package com.sip.stream.utils.zfy.voice;

import android.media.AudioAttributes;
import android.speech.tts.TextToSpeech;
import android.util.Log;

import com.google.gson.JsonObject;
import com.sip.stream.ClientApp;
import com.sip.stream.R;
import com.sip.stream.utils.zfy.ZFYUserInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TTSProvider extends AbstractBeepProvider implements TextToSpeech.OnInitListener {
    private static final String TAG = "TTSProvider";

    private GSCallBack<Boolean> initCallBack;
    private TextToSpeech textToSpeech;
    private boolean isInitSuccess = false;
    private String playUntilInit = null;
    private String playNameUntilInit = null;
    private JsonObject customToneConfig = null;
    private Pattern pattern = Pattern.compile("\\$\\((\\w+)\\)");

    public TTSProvider() {
        readCustomTone(Locale.getDefault());
    }

    @Override
    public void setUp(GSCallBack<Boolean> gSCallBack) {
        this.initCallBack = gSCallBack;
        this.textToSpeech = new TextToSpeech(ClientApp.clientApp, this);
    }

    @Override
    public void onInit(int type) {
        try {
            onInit_t(type);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void readCustomTone(Locale locale) {
        
    }

    public void onInit_t(int type) {
        if (type == 0) {
            Log.i(TAG, "init success");
            Log.i(TAG, "Locale.getDefault() = " + Locale.getDefault().toString() + " Locale.CHINA = " + Locale.CHINA.toString());
            Locale locale = Locale.getDefault();
            int language = this.textToSpeech.setLanguage(locale);
            Log.i(TAG, "setLanguage getDefault " + language + " " + locale.getLanguage());
            if (language == -1 || language == -2) {
                language = this.textToSpeech.setLanguage(Locale.CHINA);
                Log.i(TAG, "setLanguage CHINA " + language);
            }
            if (language == -1 || language == -2) {
                Log.i(TAG, "setLanguage US " + this.textToSpeech.setLanguage(Locale.US));
            }
            this.textToSpeech.setPitch(0.95f);
            this.textToSpeech.setSpeechRate(1.0f);
            this.textToSpeech.setAudioAttributes(new AudioAttributes.Builder().setLegacyStreamType(5).setUsage(5).build());
            this.isInitSuccess = true;
            if (this.playUntilInit != null) {
                Log.i(TAG, "playUntilInit isInitSuccess " + this.playUntilInit);
                playString(this.playUntilInit);
                this.playUntilInit = null;
            }
            GSCallBack<Boolean> gSCallBack = this.initCallBack;
            if (gSCallBack != null) {
                gSCallBack.onComplete(new GSResult<>());
                return;
            }
            return;
        }
        GSCallBack<Boolean> gSCallBack2 = this.initCallBack;
        if (gSCallBack2 != null) {
            gSCallBack2.onComplete(new GSResult<>(-1));
        }
        Log.i(TAG, "init failed" + type);
    }

    @Override
    public void changeAudioAttributes(boolean isMic) {
        if (this.textToSpeech == null) {
            return;
        }
        Log.i(TAG, "changeAudioAttributes");
        if (isMic)
        {
            this.textToSpeech.setAudioAttributes(new AudioAttributes.Builder().setLegacyStreamType(0).setUsage(2).setContentType(1).build());
            return;
        }
        this.textToSpeech.setAudioAttributes(new AudioAttributes.Builder().setLegacyStreamType(5).setUsage(5).build());
    }

    public void playString(String message) {
        if (message == null) {
            beepOneShot();
        } else if (this.isInitSuccess) {
            this.textToSpeech.speak(message, 1, null, UUID.randomUUID().toString());
            Log.i(TAG, "playString " + message);
        } else {
            Log.i(TAG, "playUntilInit " + message);
            if (this.playUntilInit != null) {
                this.playUntilInit += message;
            } else {
                this.playUntilInit = message;
            }
        }
    }

    @Override
    public void play(String action, String message) {
        if (message == null) {
            message = getString(action);
        }
        playString(message);
    }

    @Override
    public void release() {
        super.release();
        this.textToSpeech = null;
    }

    private String getString(String message) {
        String asString = null;
        int defaultTTSString = 0;
        Log.i(TAG, "getString for " + message);
        JsonObject jsonObject = this.customToneConfig;
        if (jsonObject != null)
        {
            try {
                asString = jsonObject.get(message).getAsString();
            } catch (Exception unused) {
            }
            if (asString == null && (defaultTTSString = getDefaultTTSString(message)) > 0) {
                asString = getContext().getString(defaultTTSString);
            }
            return asString == null ? replaceVariable(asString) : asString;
        }
        asString = null;
        if (asString == null) {
            asString = getContext().getString(defaultTTSString);
        }
        return asString;
    }


    public String replaceVariable(String message)
    {
        Matcher matcher = this.pattern.matcher(message);
        while (matcher.find()) {
            char c = 1;
            String group = matcher.group(1);
            if (group != null) {
                Log.i(TAG, "group " + group);
                if (group.startsWith("DATE_")) {
                    try {
                        String replace = group.replace("DATE_", "");
                        Log.i(TAG, "format " + replace);
                        message = message.replace(matcher.group(), new SimpleDateFormat(replace).format(new Date(System.currentTimeMillis())));
                    } catch (Exception unused) {
                        message = message.replace(matcher.group(), "");
                    }
                } else {
                    String lowerCase = group.toLowerCase();
                    lowerCase.hashCode();
                    switch (lowerCase.hashCode()) {
                        case -292215439:
                            if (lowerCase.equals("unitcode")) {
                                c = 0;
                                break;
                            }
                            c = 65535;
                            break;
                        case -291900913:
                            break;
                        case -266027976:
                            if (lowerCase.equals("usercode")) {
                                c = 2;
                                break;
                            }
                            c = 65535;
                            break;
                        case -265713450:
                            if (lowerCase.equals("username")) {
                                c = 3;
                                break;
                            }
                            c = 65535;
                            break;
                        case -79359296:
                            if (lowerCase.equals("fulldevicesn")) {
                                c = 4;
                                break;
                            }
                            c = 65535;
                            break;
                        case 3076014:
                            if (lowerCase.equals("date")) {
                                c = 5;
                                break;
                            }
                            c = 65535;
                            break;
                        case 1109192497:
                            if (lowerCase.equals("devicesn")) {
                                c = 6;
                                break;
                            }
                            c = 65535;
                            break;
                        default:
                            c = 65535;
                            break;
                    }
                    switch (c) {
                        case 0:
                            message = message.replace(matcher.group(), addSplits(ZFYUserInfo.defaultUserInfo().getUnitCode()));
                            continue;
                        case 1:
                            message = message.replace(matcher.group(), ZFYUserInfo.defaultUserInfo().getUnitName());
                            continue;
                        case 2:
                            message = message.replace(matcher.group(), addSplits(ZFYUserInfo.defaultUserInfo().getUserCode()));
                            continue;
                        case 3:
                            message = message.replace(matcher.group(), ZFYUserInfo.defaultUserInfo().getUserName());
                            continue;
                        case 4:
                            message = message.replace(matcher.group(), addSplits(ZFYUserInfo.defaultUserInfo().getVirtualSNCode()));
                            continue;
                        case 5:
                            message = message.replace(matcher.group(), new Date().toLocaleString());
                            continue;
                        case 6:
                            message = message.replace(matcher.group(), addSplits(ZFYUserInfo.defaultUserInfo().getVirtualSNCode()));
                            continue;
                        default:
                            message = message.replace(matcher.group(), "");
                            continue;
                    }
                }
            }
        }
        return message;
    }

    private String addSplits(String message) {
        Matcher matcher = Pattern.compile(".").matcher(message);
        StringBuffer stringBuffer = new StringBuffer();
        while (matcher.find()) {
            stringBuffer.append(",");
            stringBuffer.append(matcher.group());
        }
        return stringBuffer.toString();
    }

    public int getDefaultTTSString(String type) {
        return getContext().getResources().getIdentifier("speak_tts_" + type.toLowerCase(), "string", getContext().getPackageName());
    }

    @Override
    public void changeLanguage(Locale locale) {
        this.textToSpeech.setLanguage(locale);
        readCustomTone(locale);
    }

    @Override
    public void playTime(long j) {
        if (j < 1000) {
            return;
        }
        long j2 = j / 1000;
        int i = (int) (j2 / 86400);
        long j3 = j2 % 86400;
        int i2 = (int) (j3 / 3600);
        long j4 = j3 % 3600;
        int i3 = (int) (j4 / 60);
        int i4 = (int) (j4 % 60);
        String str = i > 0 ? " " + i + " " + getContext().getString(R.string.speak_tts_day) : " ";
        if (i2 > 0) {
            str = str + i2 + " " + getContext().getString(R.string.speak_tts_hour);
        }
        if (i3 > 0) {
            str = str + i3 + " " + getContext().getString(R.string.speak_tts_minute);
        }
        playString(str + i4 + " " + getContext().getString(R.string.speak_tts_second));
    }
}
